﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatabaseSchema.nvigorate;
using Nvigorate.Aspect;
using Nvigorate.Relational.Binding;
using Nvigorate.Relational.Mapping.Fluent;
using Nvigorate.Relational.State;

namespace Nvigorate.TestObjects
{
    [DatabaseLazyLoadAspect(RepositoryName="simple")]
    [StateTrackingAspect]
    public class Entry : StateTracking
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public IList<Author> Contributors { get; set; }
    }

    public class EntryMap : FluentMap<Entry>
    {
        public EntryMap()
        {
            RepositoryName = "simple";
            var table = MapTable<EntrySchema>();

            table.Assign(e => e.Id, t => t.entryId).AsKey().ReadOnly();
            table.Assign(e => e.Name, t => t.entryName);

            MapToMany<Author, ContributorSchema>(e => e.Contributors)
                .SelfToContainer(e => e.Id, c => c.entryId)
                .RelativeToContainer(a => a.Id, c => c.authorId);
        }
    }

    public class AuthorMap : FluentMap<Author>
    {
        public AuthorMap()
        {
            RepositoryName = "simple";
            var table = MapTable<AuthorSchema>();

            table.Assign(e => e.Id, t => t.authorId).AsKey().ReadOnly();
            table.Assign(e => e.Name, t => t.authorName);

            MapToMany<Entry, ContributorSchema>(a => a.Contributions)
                .SelfToContainer(a => a.Id, c => c.authorId)
                .RelativeToContainer(e => e.Id, c => c.entryId);

           
        }
    }

    [DatabaseLazyLoadAspect(RepositoryName = "simple")]
    [StateTrackingAspect]
    public class Author : StateTracking
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public IList<Entry> Contributions { get; set; }

        public Author() {}
    }

}
